/*
 * 	Additional function for tables.html
 *	Written by ThemePixels	
 *	http://themepixels.com/
 *
 *	Copyright (c) 2012 ThemePixels (http://themepixels.com)
 *	
 *	Built for Amanda Premium Responsive Admin Template
 *  http://themeforest.net/category/site-templates/admin-templates
 */

jQuery(document).ready(function(){
	var year = jQuery(".year");
    var month = jQuery(".month");
    var date = new Date();
    var y = date.getFullYear();
    //初始化年份
    year.html('');
    year.append("<option value=''>请选择年</option>");
    for (var i = 2000; i <= y; i++) {
    	year.append("<option value='"+i+"'>"+i+"</option>")
    }
    //初始化月份
    month.html('');
    month.append("<option value=''>请选择月</option>")
    for (var i = 1; i < 13; i++) {
    	month.append("<option value='"+i+"'>"+i+"</option>")
    }
	    
	jQuery(".chzn-select").chosen();
	jQuery("#bonusTime").simpleCanleder();
	jQuery('#bonusTable').dataTable({
		"sPaginationType": "full_numbers",
		"oLanguage": {
			"sLengthMenu": "每页显示 _MENU_ 条记录",
			"sZeroRecords": "抱歉， 没有找到",
			"sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
			"sInfoEmpty": "没有数据",
			"sInfoFiltered": "(从 _MAX_ 条数据中检索)",
			"sSearch":"全文搜索:",
			"oPaginate": {
			"sFirst": "首页",
			"sPrevious": "前一页",
			"sNext": "后一页",
			"sLast": "尾页",
			},
			"sZeroRecords": "没有检索到数据",
			"sProcessing": "<img src='./loading.gif' />"
		}
	});
	
	
	jQuery('#addBonusBtn').click(function(){
		jQuery('#bonus_dialog').attr("title","添加奖金信息");
		jQuery('#bonusForm').attr("action","/bonus/add");
		jQuery('#edit_btn').hide();
		jQuery('#add_btn').show();
		jQuery("#bonus_dialog").dialog({
			 width:500,
		     modal:true
		});
	});
	
	jQuery("#deptId").change(function(){
		var deptId = jQuery("#deptId").val();
		jQuery.ajax({
			type: "post",
			url: "/post/postsByDept",
			dataType: "json",
			data: {deptId: deptId},
			success: function(data) {
				console.log(data);
				jQuery("#postId").html("");
				jQuery("#postId").append("<option value=''>--请选择--</option>");
				jQuery.each(data.posts,function(i, post){
					console.log(post);
					jQuery("#postId").append("<option value='"+post.postId +"'>"+ post.postName + "</option>");
				});
			}
		})
	});
});



function confirmDelete(id) {
	jConfirm('你确定要删除这条信息吗?', '奖金', function(r) {
		if(r) {
			jQuery.ajax({
				   type: "post",
				   url: "/bonus/delete",
				   dataType:"json",
				   data: {bonusId: id},
				   success: function(data){
					   if(data.flag == true) {
						   	window.location.reload();
						   	jQuery.jGrowl("恭喜您，删除奖金成功！");
					   }else {
						   jQuery.jGrowl("很抱歉，删除奖金失败！");
					   }
				   },
					error:function(e) {  
						jQuery.jGrowl("很抱歉，删除奖金失败！");
			        }
				 });
		}
	});
	return false;
};

function showEditDialog(id){
	jQuery.ajax({
		url:'/bonus/bonusInfo',
		type:'POST',
		async:true,
		data:{
			id:id
		},
		dataType:'json',
		success:function(data){
			if(data.result.status == "success") {
				var bonus = data.result.bonus;
				console.log(bonus);
				jQuery('#bonus_dialog').attr("title","修改奖金信息");
				jQuery('#bonusForm').attr("action","/bonus/edit");
				jQuery('#edit_btn').show();
				jQuery('#add_btn').hide();
				jQuery("#bounsId").val(bonus.bonusId);
				jQuery("#bonusName").val(bonus.bonusName);
				jQuery("#bonusMoney").val(bonus.bonusMoney);
				jQuery("#bonusComment").html(bonus.bonusComment);
				jQuery("#empId_select").val(bonus.empId).trigger('liszt:updated');
				var time = bonus.bonusTime;
				var times = time.split("-");
				jQuery(".year").val(times[0]);
				jQuery(".month").val(times[1]);
				jQuery("#bonus_dialog").dialog({
					 width:500,
					 modal:true
				});
			} else {
				alert("内部错误");
			}
		}
	});
	
};